package cn.lctvu.servlet.client;

import cn.lctvu.bean.ItemNews;
import cn.lctvu.bean.Items;
import cn.lctvu.bean.PageBean;
import cn.lctvu.service.ItemNewsService;
import cn.lctvu.service.ItemService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/FindAllWithPage")
public class FindAllWithPage extends HttpServlet {
    private ItemNewsService itemNewsService=new ItemNewsService();
    private ItemService itemService=new ItemService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        (1)获得传递来的页码
       String strpageNum= request.getParameter("pageNum");
       int pageNum;
       if(strpageNum==null){
           pageNum=1;
       }else {
          pageNum= Integer.parseInt(strpageNum);
          if(pageNum<1){
              pageNum=1;
          }
       }

//
//（2）获得要查询的文章栏目id
        String stritemid=request.getParameter("itemid");
       if(stritemid==null){
           stritemid="all";
       }

//
//                (3)获得要查询的文章标题（如果想查询所有标题，文章标题默认为"")
        String title=request.getParameter("title");
       if(title==null){
           title="";
       }
//
//        (4)调用ItemNewsService中的得到分页数据的方法（getNewsWithPage），得到本页数据对象
       PageBean<ItemNews> pb=itemNewsService.getNewsWithPage(pageNum,2,stritemid,title);
//
//（5）把本页数据存在request域对象中
        request.setAttribute("pageBean",pb);
//
//（6）如果文章栏目id不为空，根据文章栏目id，得到文章栏目名称。把栏目名称放在域对象中
        if(!stritemid.equals("all")){

           Items items= itemService.findItemsById(Integer.parseInt(stritemid));
           request.setAttribute("items",items);
        }

//
//（7）得到所有栏目对象（因为在栏目显示列表中左侧需要显示所有栏目），并放在域对象中
      List<Items> listitems=  itemService.findAllItems();
        request.setAttribute("itemlist",listitems);
//
//（8）转发到list.jsp,进行显示
        request.getRequestDispatcher("/list.jsp").forward(request,response);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }
}
